<template>
  <el-row justify="end">
    <el-col :span="16" style="text-align: left;">
      <!--<el-button @click="addUser" type="primary">添加信息</el-button>-->
      <el-button @click="exportUser" type="primary">导出数据</el-button>
    </el-col>
    <el-col :span="6">
      <el-input v-model="searchInput" placeholder="请输入内容"></el-input>
    </el-col>
    <el-col :span="2">
      <el-button type="primary" @click="searchData">搜索</el-button>
    </el-col>
  </el-row>
  <el-table
    :data="
      tableData.filter(
        (data) =>
          !search || data.name.toLowerCase().includes(search.toLowerCase())
      )
    "
    :header-cell-style="{background:'#f4f3f9',color:'#606266', fontSize: '12px'}"
    :cell-style = "{fontSize: '12px'}"
    style="width: 100%; margin-top: 10px;"
  >
    <!--
    <el-table-column
      prop="id"
      label="序号"
      width="50">
    </el-table-column>
    -->
    <el-table-column prop="studentId" v-if="false"> </el-table-column>
    <el-table-column label="学生姓名" prop="studentName" width="80"> </el-table-column>
    <el-table-column label="监护人1身份证号" prop="idNum1" width="180"> </el-table-column>
    <el-table-column label="监护人1姓名" prop="parentName1" width="100"> </el-table-column>
    <el-table-column label="监护人1电话" prop="phoneNum1"> </el-table-column>
    <el-table-column label="监护人1工作单位" prop="workUnit1" width="180"> </el-table-column>
    <el-table-column label="监护人1地址" prop="address1" width="180"> </el-table-column>

    <el-table-column label="监护人2身份证号" prop="idNum2" width="180"> </el-table-column>
    <el-table-column label="监护人2姓名" prop="parentName2"   width="100"> </el-table-column>
    <el-table-column label="监护人2电话" prop="phoneNum2"  width="80"> </el-table-column>
    <el-table-column label="监护人2工作单位" prop="workUnit2" width="180"> </el-table-column>
    <el-table-column label="监护人2地址" prop="address2" width="180"> </el-table-column>
    <el-table-column label="亲情号" prop="familyNum" width="180"> </el-table-column>
    <!--<el-table-column label="操作" align="right">
      <template #default="scope">
          <el-button @click="editUser(scope.row)" size="small" type="primary">编辑</el-button>
      </template>
    </el-table-column>
    -->
  </el-table>
  <el-pagination
    small="true"
    background
    layout="prev, pager, next"
    :page-size="5"
    :total="total"
    :current-page="currentPage"
    @current-change="changePage"
    style="margin-top: 30px;">
  </el-pagination>
  <el-drawer
  title="编辑"
  v-model="drawer"
  v-show="drawer"
  :lock-scroll="false"
  :with-header="true">
      <el-form :label-position="labelPosition" label-width="120px" :model="formLabelAlign">
        <el-form-item label="姓名">
          <el-input v-model="formLabelAlign.studentName"></el-input>
        </el-form-item>
        <el-form-item label="学生身份证号">
          <el-input v-model="formLabelAlign.idNum"></el-input>
        </el-form-item>
        <el-form-item label="家长姓名">
          <el-input v-model="formLabelAlign.parentName"></el-input>
        </el-form-item>
        <el-form-item label="家长身份证号">
          <el-input v-model="formLabelAlign.parentIdNum"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="formLabelAlign.phoneNum"></el-input>
        </el-form-item>
        <el-form-item label="工作单位">
          <el-input v-model="formLabelAlign.workUnit"></el-input>
        </el-form-item>
        <el-form-item label="家挺地址">
          <el-input v-model="formLabelAlign.address"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
  </el-drawer>
</template>
<script>
import {getParent, addParent, searchParent, exportFile} from "../api/user.js"
export default {
  data() {
    return {
      tableData: [
      ],
      search: "",
      drawer: false,
      labelPosition: 'left',
      formLabelAlign: {
        userName: '', 
        cardId: '', 
        address: '', 
        phone: '', 
        subdistrict: '',
        building: '',
        block: '', 
        room: ''
      },
      queryParams: {
        page: 1,
        size: 5
      },
      total: '',
      currentPage: 1,
      searchInput: '',
      token: { authorization: localStorage.getItem("token") }
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    async getParentList() {
      this.queryParams.user = localStorage.getItem("user");
      let result = await getParent(this.queryParams);
      if(result.data.status == 'ok') {
        this.tableData = result.data.userList;
        this.total = result.data.total
        console.log(result.data.total);
      }
    },
    editUser(row) {
      this.formLabelAlign.studentName = row.studentName;
      this.formLabelAlign.idNum = row.idNum;
      this.formLabelAlign.studentClass = row.studentClass;
      this.formLabelAlign.studentMaster = row.studentMaster;
      this.formLabelAlign.masterPhone = row.masterPhone;
      this.formLabelAlign.phoneNum = row.phoneNum;
      this.formLabelAlign.studentId = row.studentId;
      this.drawer = true
    },
    async onSubmit() {
      let result = await addParent(this.formLabelAlign);
      if(result.data.status == "ok") {
        this.drawer = false;
        this.getParentList();
      }
    },
    changePage(e) {
      this.currentPage = e;
      this.queryParams.page = e;
      this.getParentList();
    },
    async searchData() {
      let result = await searchParent({'search': this.searchInput});
      console.log(result);
      if(result.data.status == 'ok') {
        this.tableData = result.data.userList;
      }
    },
    addUser() {
      this.drawer = true;
    },
    async importData() {

    },
    async exportUser() {
      let userName = localStorage.getItem("user");
      let result = await exportFile(userName);
      console.log(result);
    }
  },
  mounted() {
    this.getParentList();
  }
};
</script>
<style scoped>
</style>